package org.xjo.main;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;

public class Main_genericCount_SPORTS {


	public static String STR_READFILE="C:\\historicaldata\\xjo\\indiaodi.csv";

	//public static String STR_READFILE="C:\\historicaldata\\xjo\\windies.csv";

	//public static String STR_READFILE="C:\\historicaldata\\xjo\\aussie.csv";

	//public static String STR_READFILE="C:\\historicaldata\\xjo\\pakiodi.csv";

	//public static String STR_READFILE="C:\\historicaldata\\xjo\\xjo.csv";

	public static int PATTERNDEPTH=25;


	public static   void main(String[] args) {



		StringBuffer strBufferPattern=new StringBuffer();
		BufferedReader br=null;
		String strCurrentLine=null;

		String strCurrentLineArray[]=null;

		String strCurrentUpDown=null;

		String strPattern=null;


		ArrayList al=null;




		StringBuffer strHTMLBUffer=null;

		strHTMLBUffer=new StringBuffer();

		strHTMLBUffer.append(Constants.HTML_START.replaceAll("#", STR_READFILE));

		for(int patternCounter=PATTERNDEPTH;patternCounter>7;patternCounter--){

		try{




			br=	new BufferedReader (new FileReader(STR_READFILE));
			al=new ArrayList();



			while((strCurrentLine=br.readLine())!=null){
              if(strCurrentLine!=null&&strCurrentLine.indexOf(',')>0){
            	  al.add(strCurrentLine);
            	  strCurrentLineArray=strCurrentLine.split(",");
            	  //System.out.println(strCurrentLineArray[strCurrentLineArray.length-1]);
            	  strCurrentUpDown=strCurrentLineArray[strCurrentLineArray.length-1];

            	 if(true){
            	  //if(strCurrentUpDown.substring(0, 1).equalsIgnoreCase("w")||strCurrentUpDown.substring(0, 1).equalsIgnoreCase("l")){
            	  if(strCurrentUpDown.indexOf("win")>=0){
            		  strBufferPattern.append("U");
            	  }else{
            		  strBufferPattern.append("D");
            	  }
            	  }

            	  //strBufferPattern.append(strCurrentUpDown.substring(0, 1));

              }

			}
			//Eo While

			System.out.println(strBufferPattern.toString());
			//System.exit(0);




			strPattern=strBufferPattern.substring(0, patternCounter);

			System.out.println(strPattern);



			//System.exit(0);

			if(strBufferPattern.indexOf(strPattern,1)>0&&strBufferPattern.indexOf(strPattern,1)<al.size()-strPattern.length()){

				strHTMLBUffer.append(Constants.TABLE_START.replaceAll("#", "CURRENT - "+patternCounter));

			int nextIndex=0;

			//System.out.println(al.get(nextIndex-1));

			for(int i=0;i<patternCounter;i++){
				System.out.println(al.get(i));

				strHTMLBUffer.append(Constants.TABLE_ROW.replaceAll("#", al.get(i).toString()));
			}

			strHTMLBUffer.append(Constants.TABLE_END);

			 nextIndex=patternCounter-1;


			//System.out.println(strBufferPattern.indexOf(strFiver,nextIndex));

			 strHTMLBUffer.append("<br><br>");





			while(strBufferPattern.indexOf(strPattern,nextIndex)>3&&strBufferPattern.indexOf(strPattern,nextIndex)<al.size()-patternCounter){




				System.out.println("---Finding------------");

				strHTMLBUffer.append(Constants.TABLE_START.replaceAll("#", "FINDING -"+ patternCounter));

				System.out.println(strBufferPattern.indexOf(strPattern,nextIndex));

				strHTMLBUffer.append(Constants.TABLE_ROW.replaceAll("#",strBufferPattern.indexOf(strPattern,nextIndex)+""));
				nextIndex=strBufferPattern.indexOf(strPattern,nextIndex);

				System.out.println(al.get(nextIndex-2));

				strHTMLBUffer.append(Constants.TABLE_ROW.replaceAll("#",al.get(nextIndex-2)+""));
				System.out.println(" "+al.get(nextIndex-1));
				strHTMLBUffer.append(Constants.TABLE_ROW.replaceAll("#",al.get(nextIndex-1)+""));
				System.out.println("");
				strHTMLBUffer.append(Constants.TABLE_ROW.replaceAll("#","<b>FUTURE</b>"));
				for(int i=0;i<patternCounter;i++){
					System.out.println(al.get(nextIndex+i));

					strHTMLBUffer.append(Constants.TABLE_ROW.replaceAll("#",al.get(nextIndex+i)+""));
				}



				nextIndex+=1;


				System.out.println("---EOFinding------------");

				strHTMLBUffer.append(Constants.TABLE_END);


			}


			strHTMLBUffer.append("<br><br><hr><br><br>");
			}





		}catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		}
		finally{
			try{
				br.close();
			}catch (Exception e) {
				e.printStackTrace();
				// TODO: handle exception
			}
		}



		}

		strHTMLBUffer.append(Constants.HTML_END);

		BufferedWriter bWriter=null;
		try{
			bWriter=new BufferedWriter(new FileWriter(Constants.PAK_PATH));

			bWriter.write(strHTMLBUffer.toString());
			}catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
			finally{
				try{
				bWriter.close();
				}catch (Exception e) {
					// TODO: handle exception
				}
			}




	}
}
